package cn.tedu.tmall.front.mall.dao.persist.search.impl;
import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsElasticSearchRepository extends
        ElasticsearchRepository<GoodsSearchPO, Long> {

     //https://docs.spring.io/spring-data/elasticsearch/docs/4.2.1/reference/html/#elasticsearch.query-methods.criterions
     //可以根据elasticsearch提供的方法,不需要写实体类,缺点是不便于复杂的查询,方法名是固定的,
     // 随着查询条件的增加,方法名也越来越长,不便于方法的调用
     List<GoodsSearchPO> findByTitle(String x);

     //可以自定义查询来解决复杂的查询,查询条件详见test/request/specifySearchRequest类中的查询
     @Query("{\n" +
             "    \"bool\": {\n" +
             "      \"must\": [\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"title\": \"?0\"\n" +
             "          }\n" +
             "        },\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"brief\": \"?1\"\n" +
             "          }\n" +
             "        }\n" +
             "      ]\n" +
             "    }\n" +
             "  }")
     List<GoodsSearchPO> test(String x,String y);

     //对查询分页查询,但page分页中拿不到查询的总数,可以用SearchPage
     @Query("{\n" +
             "    \"match\": {\n" +
             "      \"title\": \"?0\"\n" +
             "    }\n" +
             "  }")
     Page<GoodsSearchPO> testPage(String x, Pageable pageable);

     //对查询分页查询,但page分页中拿不到查询的总数,可以用SearchPage
     @Query("{\n" +
             "    \"match\": {\n" +
             "      \"title\": \"?0\"\n" +
             "    }\n" +
             "  }")
     SearchPage<GoodsSearchPO> testSearchPage(String x, Pageable pageable);

     //对查询结果配置高亮关键字
     @Query("{\n" +
             "    \"bool\": {\n" +
             "      \"should\": [\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"title\": \"?0\"\n" +
             "          }\n" +
             "        },\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"brief\": \"?0\"\n" +
             "          }\n" +
             "        },\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"barcode\": \"?0\"\n" +
             "          }\n" +
             "        },\n" +
             "        {\n" +
             "          \"match\": {\n" +
             "            \"keyword\": \"?0\"\n" +
             "          }\n" +
             "        },{\n" +
             "          \"match\": {\n" +
             "            \"categoryName\": \"?0\"\n" +
             "          }\n" +
             "        }\n" +
             "      ]\n" +
             "    }\n" +
             "  }")
     @Highlight(fields = {@HighlightField(name = "title")},
             parameters = @HighlightParameters(preTags = "<font style:'color:red;'>",postTags = "</font>"))
     SearchPage<GoodsSearchPO> customSearchPage(String x, Pageable pageable);



}
